﻿#region changelog
/*
 * 11/22/11 - as (Prodigy Software Ltd): Original code
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using Durrant.Plugins.TvDB;

namespace Durrant.Plugins
{
    public class CryptographyHelper
    {
        public static byte[] Encrypt(byte[] unencryptedByteArray, byte[] encryptionKey, byte[] vector)
        {
            MemoryStream memoryStream = new MemoryStream();

            Rijndael rijndael = Rijndael.Create();

            rijndael.Key = encryptionKey;
            rijndael.IV = vector;

            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(unencryptedByteArray, 0, unencryptedByteArray.Length);

            cryptoStream.Close();

            byte[] encryptedByteArray = memoryStream.ToArray();

            return encryptedByteArray;
        }

        public static string Encrypt(string unencryptedString, string password)
        {
            byte[] unencryptedByteArray = System.Text.Encoding.Unicode.GetBytes(unencryptedString);

            byte[] saltByteArray = Encoding.Unicode.GetBytes(Constants.SALT);

            PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(password, saltByteArray);

            byte[] encryptedByteArray = Encrypt(unencryptedByteArray, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));

            return Convert.ToBase64String(encryptedByteArray);
        }

        public static byte[] Decrypt(byte[] encryptedByteArray, byte[] encryptionKey, byte[] vector)
        {
            MemoryStream memoryStream = new MemoryStream();

            Rijndael rijndael = Rijndael.Create();

            rijndael.Key = encryptionKey;
            rijndael.IV = vector;

            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(encryptedByteArray, 0, encryptedByteArray.Length);

            cryptoStream.Close();

            byte[] decryptedByteArray = memoryStream.ToArray();

            return decryptedByteArray;
        }

        public static string Decrypt(string encryptedString, string Password)
        {
            byte[] encryptedByteArray = Convert.FromBase64String(encryptedString);

            byte[] saltByteArray = Encoding.Unicode.GetBytes(Constants.SALT);

            PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(Password, saltByteArray);

            byte[] decryptedData = Decrypt(encryptedByteArray, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));

            return Encoding.Unicode.GetString(decryptedData);
        }

        public static byte[] Decrypt(byte[] encryptedByteArray, string Password)
        {
            byte[] saltByteArray = Encoding.Unicode.GetBytes(Constants.SALT);

            PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(Password, saltByteArray);

            return Decrypt(encryptedByteArray, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));
        }
    }
}